package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.AdminUser;
import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.Practitioner;
import com.banksoft.cps.bean.User;
import com.banksoft.cps.bean.common.ErrorTip;
import com.banksoft.cps.bean.query.PractitionerQuery;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.enums.UserType;
import com.banksoft.cps.service.PractitionerService;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.StringUtil;
import com.banksoft.cps.util.UserContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2017/12/28.
 */
@Controller
@RequestMapping("/admin/practitioner/")
public class AdminPractitionerController extends ControllerConstants {

    @Autowired
    PractitionerService practitionerService;

    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(PractitionerQuery query){
        List<Practitioner> list =  practitionerService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(PractitionerQuery query){
        long count =  practitionerService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        Practitioner bean = practitionerService.findById(id);
        return toJSONString(createDataTip(bean));
    }
    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Practitioner bean, HttpServletRequest request){
        try {
            User user = (User) UserContext.getUser();
            bean.setCreator(UserType.security.name());
            bean.setCreateAccount(user.getAccount());
            practitionerService.save(bean);

            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "setQDoc",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String setQDoc(String id,String qualificationDoc){
        practitionerService.setQDoc(id,qualificationDoc);
        return toJSONString(createDataTip(SUCCESS));
    }

    @RequestMapping(value = "updateStatus",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateStatus(String ids, String status){
        if(StringUtil.isNullOrEmpty(ids)){
            return toJSONString(new ErrorTip(1,"请选择要注销的人员"));
        }
        practitionerService.updateStatus(ids.split(","),status);
        return toJSONString(SUCCESS);
    }
    @RequestMapping(value = "delete",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(String ids){
        if(StringUtil.isNullOrEmpty(ids)){
            return toJSONString(new ErrorTip(1,"请选择要删除的人员"));
        }
        practitionerService.delete(ids.split(","));
        return toJSONString(SUCCESS);
    }
}
